package com.eduction.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.eduction.bean.FeedBackBean;
import com.eduction.common.utils.RegularExpressionsTool;
import com.eduction.modules.system.entity.*;
import com.eduction.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eduction.bean.SessionUser;
import com.eduction.common.ReturnCode;
import com.eduction.resource.BaseController;

@Controller
@RequestMapping("/expertTable")
public class ExpertTableController extends BaseController {
	private static final Logger logger = Logger.getLogger(ExpertTableController.class);
	@Autowired
	private CourseAnalyseMapService courseAnalyseMapService;
	@Autowired
	private CourseRecordMapService courseRecordMapService;
	@Autowired 
	private InterviewAnalyseMapService interviewAnalyseMapService;
	@Autowired
	private CourseAnalyseService courseAnalyseService;
	@Autowired
	private CourseRecordServie courseRecordServie;
	@Autowired
	private InterviewAnalyseService interviewAnalyseService;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private FeedBackService feedBackService;
	@Autowired
	private ObservationService observationService;
	/**
	 * 添加随堂听课记录空表
	 */
	@RequestMapping(value = "/addCRMap" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> addCRMap(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		
		String schoolId = map.get("schoolId");
		String sign = map.get("sign");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		if(sign == null || StringUtils.isBlank(sign)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "标识不能为空");
		}
		try {
			CourseRecordMap courseRecordMap = this.courseRecordMapService.queryCourseRecordById(sign,schoolId,expertId);
			if(courseRecordMap!=null){
				return toResponse(null, ReturnCode.ACCOUNT_HAVE_EXIST, "该学校对应的标识已经存在");
			}
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "校验编号异常");
		}

		try {
			CourseRecordMap crm = new CourseRecordMap();
			crm.setExpertId(expertId);
			crm.setSchoolId(schoolId);
			crm.setSign(sign);
			crm.setState("0");
			this.courseRecordMapService.saveCourseRecordMap(crm);
			
			/*CourseRecord courseRecord = new CourseRecord();
			courseRecord.setExpertId(expertId);
			courseRecord.setSchoolId(schoolId);
			courseRecord.setSign(sign);
			String cid = this.courseRecordServie.saveCourseRecord(courseRecord);*/
			return toResponse(null, ReturnCode.SUCCESS, "添加随堂听课记录表成功");
		} catch (RuntimeException e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "添加随堂听课记录表失败");
		}
	}
	
	/**
	 * 查看某个随堂听课记录信息
	 */
	@RequestMapping(value = "/queryOneCR" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryOneCR(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		String identity = (String) request.getSession().getAttribute(SessionUser.IDENTITY);//1:组员  2：组长 3：联络员

		String id = map.get("sign");
		String schoolId = map.get("schoolId");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "schoolId不能为空");
		}
		if(id == null || StringUtils.isBlank(id)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "sign不能为空");
		}
		try {
			CourseRecord courseRecord=null;
			if("3".equals(identity)||"10000".equals(expertId)){
				courseRecord = this.courseRecordServie.queryCourseRecordById(id, schoolId);

			}else {
				courseRecord = this.courseRecordServie.queryCourseRecordById(id, schoolId, expertId);
			}
			return toResponse(courseRecord, ReturnCode.SUCCESS, "查看某个随堂听课记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查看某个随堂听课记录表失败");
		}
	}
	
	/**
	 * 保存随堂听课记录信息
	 */
	@RequestMapping(value = "/saveCourseRecord" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveCourseRecord(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

	   	   String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
		 
		 String schoolId = map.get("schoolId");//学校id
		 String kind = map.get("kind");//参评类别
		 String reportingTime = map.get("reportingTime");//申报时间
		 String teacher = map.get("teacher");//教师
		 String subject = map.get("subject");//学科
		 String classs = map.get("classs");//班级
		 String teachingTime = map.get("teachingTime");//任教时间
		 String weekClassHours = map.get("weekClassHours");//周课时数
		if(weekClassHours!=null&&!weekClassHours.isEmpty()&&!RegularExpressionsTool.isPositiveInteger(weekClassHours)){
			return toResponse(null, ReturnCode.FAIL, "周课时数不合法");
		}
		 String technicalTitle = map.get("technicalTitle");//技术职称
		 String teachContent = map.get("teachContent");//教学内容
		 String level = map.get("level");//评价等级
		if(level==null){
			return toResponse(null, ReturnCode.FAIL, "评价等级不能为空");
		}
		int levelSign=0;
		if("优秀".equals(level)){
			levelSign=1;
		}
		if("良好".equals(level)){
			levelSign=1;
		}
		if("一般".equals(level)){
			levelSign=1;
		}
		if("较差".equals(level)){
			levelSign=1;
		}
		if(levelSign==0){
			return toResponse(null, ReturnCode.FAIL, "请根据提示输入评价等级");
		}
		 String sign = map.get("sign");
		 String generalComment = map.get("generalComment");//总体评价与建议
		if(null == expertId){
				return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		try {
			CourseRecord cr = new CourseRecord();
			cr.setSchoolId(schoolId);
			cr.setExpertId(expertId);
			cr.setKind(kind);
			cr.setReportingTime(reportingTime);
			cr.setTeacher(teacher);
			cr.setSubject(subject);
			cr.setClasss(classs);
			cr.setTeachingTime(teachingTime);
			if("".equals(weekClassHours)){
				cr.setWeekClassHours(null);
			}else{
				cr.setWeekClassHours(weekClassHours);
			}
			cr.setTechnicalTitle(technicalTitle);
			cr.setTeachContent(teachContent);
			cr.setLevel(level);
			cr.setSign(sign);
			cr.setGeneralComment(generalComment);
			this.courseRecordServie.saveCourseRecord(cr);
			//修改关系表中填写状态
			this.courseRecordMapService.updateCourseRecordMapState(cr.getSign(),cr.getSchoolId(),cr.getExpertId());
			return toResponse(null, ReturnCode.SUCCESS, "填写随堂听课记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "填写随堂听课记录表失败");
		}
	}

	/**
	 * 修改随堂听课记录信息
	 */
	@RequestMapping(value = "/updateCourseRecord" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateCourseRecord(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}


		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
		String pId = map.get("pId");//主键编号

		String kind = map.get("kind");//参评类别
		String reportingTime = map.get("reportingTime");//申报时间
		String teacher = map.get("teacher");//教师
		String subject = map.get("subject");//学科
		String classs = map.get("classs");//班级
		String teachingTime = map.get("teachingTime");//任教时间
		String weekClassHours = map.get("weekClassHours");//周课时数
		if(weekClassHours!=null&&!weekClassHours.isEmpty()&&!RegularExpressionsTool.isPositiveInteger(weekClassHours)){
			return toResponse(null, ReturnCode.FAIL, "周课时数不合法");
		}
		String technicalTitle = map.get("technicalTitle");//技术职称
		String teachContent = map.get("teachContent");//教学内容
		String level = map.get("level");//评价等级
		if(level==null){
			return toResponse(null, ReturnCode.FAIL, "评价等级不能为空");
		}
		int levelSign=0;
		if("优秀".equals(level)){
			levelSign=1;
		}
		if("良好".equals(level)){
			levelSign=1;
		}
		if("一般".equals(level)){
			levelSign=1;
		}
		if("较差".equals(level)){
			levelSign=1;
		}
		if(levelSign==0){
			return toResponse(null, ReturnCode.FAIL, "请根据提示输入评价等级");
		}
		String generalComment = map.get("generalComment");//总体评价与建议
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		try {
			CourseRecord cr =courseRecordServie.queryCRsById(pId) ;
			cr.setExpertId(expertId);
			cr.setKind(kind);
			cr.setReportingTime(reportingTime);
			cr.setTeacher(teacher);
			cr.setSubject(subject);
			cr.setClasss(classs);
			cr.setTeachingTime(teachingTime);
			if("".equals(weekClassHours)){
				cr.setWeekClassHours(null);
			}else{
				cr.setWeekClassHours(weekClassHours);
			}
			cr.setTechnicalTitle(technicalTitle);
			cr.setTeachContent(teachContent);
			cr.setLevel(level);
			cr.setGeneralComment(generalComment);
			this.courseRecordServie.saveCourseRecord(cr);

			return toResponse(null, ReturnCode.SUCCESS, "修改随堂听课记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "修改随堂听课记录表失败");
		}
	}

	/**
	 * 删除随堂听课记录信息
	 */
	@RequestMapping(value = "/deleteCourseRecord" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> deleteCourseRecord(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}


		String pid = map.get("pid");
		CourseRecordMap crm=courseRecordMapService.findByPid(pid);
			try {
				courseRecordServie.deleteCourseRecordMapState(crm.getSign(), crm.getSchoolId(), crm.getExpertId());
				courseRecordMapService.deleteByPid(pid);
				return toResponse(null, ReturnCode.SUCCESS, "删除随堂听课记录表成功");
			}catch (Exception e) {
				logger.error(e);
				return toResponse(null, ReturnCode.FAIL, "删除随堂听课记录表失败");
			}
	}
	
	/**
	 * 查询这个专家下的所有随堂听课记录表
	 */
	@RequestMapping(value = "/queryCRsByExpert" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryCRsByExpert(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
        String identity = (String) request.getSession().getAttribute(SessionUser.IDENTITY);//1:组员  2：组长 3：联络员
	
		if(null == expertId){
				return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
	
		try {
			List<CourseRecordMap> crs = this.courseRecordMapService.queryMapsByExpert(expertId,identity);
			return toResponse(crs, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}
	
	
	/**
	 * 添加课程剖析记录空表
	 */
	@RequestMapping(value = "/addCAMap" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> addCAMap(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		
		String schoolId = map.get("schoolId");
		String sign = map.get("sign");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(sign == null || StringUtils.isBlank(sign)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "标识不能为空");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}

		try {
			CourseAnalyseMap courseAnalyseMap = this.courseAnalyseMapService.queryCourseAnalyseById(sign,schoolId,expertId);
			if(courseAnalyseMap!=null){
				return toResponse(null, ReturnCode.ACCOUNT_HAVE_EXIST, "该学校对应的标识已经存在");
			}
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "校验编号异常");
		}
		try {
			CourseAnalyseMap cam = new CourseAnalyseMap();
			cam.setExpertId(expertId);
			cam.setSchoolId(schoolId);
			cam.setSign(sign);
			cam.setState("0");
			this.courseAnalyseMapService.saveCourseAnalyseMap(cam);
			
			/*CourseAnalyse ca = new CourseAnalyse();
			ca.setExpertId(expertId);
			ca.setSchoolId(schoolId);
			ca.setSign(sign);
			String cid = this.courseAnalyseService.saveCourseAnalyse(ca);*/
			return toResponse(null, ReturnCode.SUCCESS, "添加课程剖析记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "添加课程剖析记录表失败");
		}
	}
	
	/**
	 * 保存课堂剖析记录信息
	 */
	@RequestMapping(value = "/saveCourseAnalyse" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveCourseAnalyse(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		   String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
		
		 String schoolId = map.get("schoolId");//学校id
		 String kind= map.get("kind");//参评类别
		 String reportingTime= map.get("reportingTime");//申报时间
		 String talkCourse= map.get("talkCourse");//说课人
		 String courseName= map.get("courseName");//课程名称
		 String technicalTitle= map.get("technicalTitle");//技术职称
		 String post= map.get("post");//职务
		 String sign = map.get("sign");
		 String talkCourseContent= map.get("talkCourseContent");//说课内容
		 String generalComment= map.get("generalComment");//总体评价
		 String suggest= map.get("suggest");//建议 
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		try {
			CourseAnalyse ca = new CourseAnalyse();
			ca.setExpertId(expertId);
			ca.setSchoolId(schoolId);
			ca.setKind(kind);
			ca.setReportingTime(reportingTime);
			ca.setTalkCourse(talkCourse);
			ca.setTechnicalTitle(technicalTitle);
			ca.setCourseName(courseName);
			ca.setPost(post);
			ca.setSign(sign);
			ca.setTalkCourseContent(talkCourseContent);
			ca.setGeneralComment(generalComment);
			ca.setSuggest(suggest);
			this.courseAnalyseService.saveCourseAnalyse(ca);
			//修改关系表中填写状态
			this.courseAnalyseMapService.updateCourseAnalyseMapState(ca.getSign(), ca.getSchoolId(), ca.getExpertId());
			return toResponse(null, ReturnCode.SUCCESS, "填写课程剖析记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "填写课程剖析记录表失败");
		}
	}

	/**
	 * 修改课堂剖析记录信息
	 */
	@RequestMapping(value = "/updateCourseAnalyse" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateCourseAnalyse(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}


		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id

		String pId = map.get("pid");//主键id
		String kind= map.get("kind");//参评类别
		String reportingTime= map.get("reportingTime");//申报时间
		String talkCourse= map.get("talkCourse");//说课人
		String courseName= map.get("courseName");//课程名称
		String technicalTitle= map.get("technicalTitle");//技术职称
		String post= map.get("post");//职务
		String talkCourseContent= map.get("talkCourseContent");//说课内容
		String generalComment= map.get("generalComment");//总体评价
		String suggest= map.get("suggest");//建议
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		try {
			CourseAnalyse ca = courseAnalyseService.findById(pId);
			ca.setKind(kind);
			ca.setReportingTime(reportingTime);
			ca.setTalkCourse(talkCourse);
			ca.setTechnicalTitle(technicalTitle);
			ca.setCourseName(courseName);
			ca.setPost(post);
//			ca.setSign(sign);
			ca.setTalkCourseContent(talkCourseContent);
			ca.setGeneralComment(generalComment);
			ca.setSuggest(suggest);
			this.courseAnalyseService.saveCourseAnalyse(ca);

			return toResponse(null, ReturnCode.SUCCESS, "修改课程剖析记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "修改课程剖析记录表失败");
		}
	}


	/**
	 * 查看某个课堂剖析记录表
	 */
	@RequestMapping(value = "/queryOneCA" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryOneCA(HttpServletRequest request,@RequestBody Map<String,String> map){

		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		String identity = (String) request.getSession().getAttribute(SessionUser.IDENTITY);//1:组员  2：组长 3：联络员
		String id = map.get("sign");
		String schoolId = map.get("schoolId");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		
		if(id == null || StringUtils.isBlank(id)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "sign不能为空");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "schoolId不能为空");
		}
		try {
			CourseAnalyse courseAnalyse = null;
			if("3".equals(identity)||"10000".equals(expertId)){
				courseAnalyse = this.courseAnalyseService.queryCourseAnalyseById(id,schoolId);

			}else {
				courseAnalyse = this.courseAnalyseService.queryCourseAnalyseById(id,expertId,schoolId);
			}


			return toResponse(courseAnalyse, ReturnCode.SUCCESS, "查看某个课堂剖析记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查看某个课堂剖析记录表失败");
		}
	}
	
	/**
	 * 查询这个专家下的所有课堂剖析记录表
	 */
	@RequestMapping(value = "/queryCAsByExpert" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryCAsByExpert(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		   String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
        String identity = (String) request.getSession().getAttribute(SessionUser.IDENTITY);//1:组员  2：组长 3：联络员
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
	
		try {
			List<CourseAnalyseMap> cas = this.courseAnalyseMapService.queryMapByExpert(expertId,identity);
			return toResponse(cas, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}

	/**
	 * 删除课堂剖析记录信息
	 */
	@RequestMapping(value = "/deleteCourseAnalyse" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> deleteCourseAnalyse(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String pid = map.get("pid");
		CourseAnalyseMap cam=courseAnalyseMapService.findByPid(pid);
		try {
			courseAnalyseService.deleteCourseAnalyseMapState(cam.getSign(), cam.getSchoolId(), cam.getExpertId());
			courseAnalyseMapService.deleteByPid(pid);
			return toResponse(null, ReturnCode.SUCCESS, "删除某个课堂剖析记录表成功");
		}catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "删除某个课堂剖析记录表失败");
		}
	}



	/**
	 * 添加访谈座谈记录空表
	 */
	@RequestMapping(value = "/addIAMap" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> addIAMap(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		
		String schoolId = map.get("schoolId");
		String sign = map.get("sign");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		if(sign == null || StringUtils.isBlank(sign)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "标识不能为空");
		}
		try {
			InterviewAnalyseMap interviewAnalyseMap = this.interviewAnalyseMapService.queryIsExist(sign,schoolId,expertId);
			if(interviewAnalyseMap!=null){
				return toResponse(null, ReturnCode.ACCOUNT_HAVE_EXIST, "该学校对应的标识已经存在");
			}
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "校验编号异常");
		}
		try {
			InterviewAnalyseMap cam = new InterviewAnalyseMap();
			cam.setExpertId(expertId);
			cam.setSchoolId(schoolId);
			cam.setSign(sign);
			cam.setState("0");
			this.interviewAnalyseMapService.saveInterviewAnalyseMap(cam);
			
		/*	InterviewAnalyse ia = new InterviewAnalyse();
			ia.setExpertId(expertId);
			ia.setSchoolId(schoolId);
			ia.setSign(sign);
			String cid = this.interviewAnalyseService.saveInterviewAnalyse(ia);*/
			return toResponse(null, ReturnCode.SUCCESS, "添加访谈座谈记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "添加访谈座谈记录表失败");
		}
	}
	
	/**
	 * 保存访谈座谈记录信息
	 */
	@RequestMapping(value = "/saveInterviewAnalyse" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveInterviewAnalyse(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}


		    String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
		
		 String schoolId = map.get("schoolId");//学校id
		 String kind = map.get("kind");//参评类别
		if(kind==null||"".equals(kind)){
			return toResponse(null, ReturnCode.FAIL, "参评类别不能为空!");
		}
		 String sign = map.get("sign");
		 String reportingTime = map.get("reportingTime");//申报时间
		if(reportingTime==null||"".equals(reportingTime)){
			return toResponse(null, ReturnCode.FAIL, "申报时间不能为空!");
		}
		 String identity1 = map.get("identity1");//身份1
		if(identity1==null||"".equals(identity1)){
			return toResponse(null, ReturnCode.FAIL, "第一项身份不能为空!");
		}
		 String interview1 = map.get("interview1");//访谈重点1
		if(interview1==null||"".equals(interview1)){
			return toResponse(null, ReturnCode.FAIL, "第一项访谈重点不能为空!");
		}
		 String dept1 = map.get("dept1");//部门1
		if(dept1==null||"".equals(dept1)){
			return toResponse(null, ReturnCode.FAIL, "第一项部门不能为空!");
		}
		 String identity2 = map.get("identity2");//身份2
		 String interview2 = map.get("interview2");//访谈重点2
		 String dept2 = map.get("dept2");//部门2
		 String identity3 = map.get("identity3");//身份3
		 String interview3 = map.get("interview3");//访谈重点3
		 String dept3 = map.get("dept3");//部门3
		 String suggest = map.get("suggest");//建议
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		try {
			InterviewAnalyse a = new InterviewAnalyse();
			a.setSchoolId(schoolId);
			a.setExpertId(expertId);
			a.setKind(kind);
			a.setSign(sign);
			a.setReportingTime(reportingTime);
			a.setIdentity1(identity1);
			a.setIdentity2(identity2);
			a.setIdentity3(identity3);
			a.setInterview1(interview1);
			a.setInterview2(interview2);
			a.setInterview3(interview3);
			a.setDept1(dept1);
			a.setDept2(dept2);
			a.setDept3(dept3);
			a.setSuggest(suggest);
			String aid = this.interviewAnalyseService.saveInterviewAnalyse(a);
			//修改关系表中填写状态
			this.interviewAnalyseMapService.updateInterviewAnalyseMapState(a.getSign(), a.getSchoolId(), a.getExpertId());
			return toResponse(aid, ReturnCode.SUCCESS, "填写访谈座谈记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "填写访谈座谈记录表失败");
		}
	}


	/**
	 * 修改访谈座谈记录信息
	 */
	@RequestMapping(value = "/updateInterviewAnalyse" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateInterviewAnalyse(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id

		String pId = map.get("pId");//主键id
		String kind = map.get("kind");//参评类别
//		String sign = map.get("sign");
		if(kind==null||"".equals(kind)){
			return toResponse(null, ReturnCode.FAIL, "参评类别不能为空!");
		}
		String reportingTime = map.get("reportingTime");//申报时间
		if(reportingTime==null||"".equals(reportingTime)){
			return toResponse(null, ReturnCode.FAIL, "申报时间不能为空!");
		}
		String identity1 = map.get("identity1");//身份1
		if(identity1==null||"".equals(identity1)){
			return toResponse(null, ReturnCode.FAIL, "第一项身份不能为空!");
		}
		String interview1 = map.get("interview1");//访谈重点1
		if(interview1==null||"".equals(interview1)){
			return toResponse(null, ReturnCode.FAIL, "第一项访谈重点不能为空!");
		}
		String dept1 = map.get("dept1");//部门1
		if(dept1==null||"".equals(dept1)){
			return toResponse(null, ReturnCode.FAIL, "第一项部门不能为空!");
		}
		String identity2 = map.get("identity2");//身份2
		String interview2 = map.get("interview2");//访谈重点2
		String dept2 = map.get("dept2");//部门2
		String identity3 = map.get("identity3");//身份3
		String interview3 = map.get("interview3");//访谈重点3
		String dept3 = map.get("dept3");//部门3
		String suggest = map.get("suggest");//建议
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}

//		if(schoolId == null || StringUtils.isBlank(schoolId)){
//			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
//		}
		try {
			InterviewAnalyse a = interviewAnalyseService.findById(pId);
//			a.setSchoolId(schoolId);
			a.setExpertId(expertId);
			a.setKind(kind);
//			a.setSign(sign);
			a.setReportingTime(reportingTime);
			a.setIdentity1(identity1);
			a.setIdentity2(identity2);
			a.setIdentity3(identity3);
			a.setInterview1(interview1);
			a.setInterview2(interview2);
			a.setInterview3(interview3);
			a.setDept1(dept1);
			a.setDept2(dept2);
			a.setDept3(dept3);
			a.setSuggest(suggest);
			String aid = this.interviewAnalyseService.saveInterviewAnalyse(a);
//			//修改关系表中填写状态
//			this.interviewAnalyseMapService.updateInterviewAnalyseMapState(a.getSign(), a.getSchoolId(), a.getExpertId());
			return toResponse(aid, ReturnCode.SUCCESS, "修改访谈座谈记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "修改访谈座谈记录表失败");
		}
	}
	/**
	 * 查看某个访谈座谈记录表
	 */
	@RequestMapping(value = "/queryOneIA" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryOneIA(HttpServletRequest request,@RequestBody Map<String,String> map){

		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		String identity = (String) request.getSession().getAttribute(SessionUser.IDENTITY);//1:组员  2：组长 3：联络员

		String id = map.get("sign");
		String schoolId = map.get("schoolId");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		
		if(id == null || StringUtils.isBlank(id)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "sign不能为空");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "schoolId不能为空");
		}
		try {
			InterviewAnalyse interviewAnalyse = null;
			if("3".equals(identity)||"10000".equals(expertId)){
				interviewAnalyse = this.interviewAnalyseService.queryInterviewAnalyseById(id,schoolId);

			}else {
				interviewAnalyse =this.interviewAnalyseService.queryInterviewAnalyseById(id,expertId,schoolId);
			}
			return toResponse(interviewAnalyse, ReturnCode.SUCCESS, "查看某个访谈座谈记录表成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查看某个访谈座谈记录表失败");
		}
	}


	/**
	 * 删除访谈座谈记录表
	 */
	@RequestMapping(value = "/deleteInterviewAnalyse" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> deleteInterviewAnalyse(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String pid = map.get("pid");
		InterviewAnalyseMap iam=interviewAnalyseMapService.findByPid(pid);
		try {
            interviewAnalyseService.deleteInterviewAnalyseMapState(iam.getSign(), iam.getSchoolId(), iam.getExpertId());
            interviewAnalyseMapService.deleteByPid(pid);
			return toResponse(null, ReturnCode.SUCCESS, "删除某个访谈座谈记录表成功");
		}catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "删除某个访谈座谈记录表失败");
		}
	}
	
	/**
	 * 查询这个专家下的所有访谈座谈记录表
	 */
	@RequestMapping(value = "/queryIAsByExpert" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryIAsByExpert(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
        String identity = (String) request.getSession().getAttribute(SessionUser.IDENTITY);//1:组员  2：组长 3：联络员
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
	
		try {
			List<InterviewAnalyseMap> ias = this.interviewAnalyseMapService.queryMapsByExpert(expertId,identity);
			return toResponse(ias, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}
	
	/**
	 * 添加之前，查询这个专家所在组绑定的学校
	 */
	@RequestMapping(value = "/querySchoolByExpert" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> querySchoolByExpert(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		   String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
	
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
	
		try {
			List<School> schools = this.schoolService.querySchoolByExpertOne(expertId);
			return toResponse(schools, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}
	
	/**
	 * 添加评估反馈意见空表
	 */
	@RequestMapping(value = "/addFeedBack" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> addFeedBack(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		
		String schoolId = map.get("schoolId");
		String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		try {
			FeedBack feedback = new FeedBack();
			feedback.setExpertId(expertId);
			feedback.setSchoolId(schoolId);
			feedback.setCreateTime(createTime);
			feedback.setState("0");
			String id = this.feedBackService.saveFeedBack(feedback);
			return toResponse(id, ReturnCode.SUCCESS, "创建空表成功");
		} catch (RuntimeException e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "创建空表失败");
		}
	}
	
	/**
	 * 查询这个专家下的所有评估反馈意见表
	 */
	@RequestMapping(value = "/queryFBsByExpert" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryFBsByExpert(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
	
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
	
		try {
			List<FeedBack> feedbacks = this.feedBackService.queryFBsByExpert(expertId);
			return toResponse(feedbacks, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}
	

	/**
	 * 保存评估反馈意见表
	 */
	@RequestMapping(value = "/saveFeedBack" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveFeedBack(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		   String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
		
		 String schoolId = map.get("schoolId");
		 //String id = map.get("id");
		 String survey = map.get("survey");//概况
		 String impression = map.get("impression");//印象
		 String characteristic = map.get("characteristic");//特色
		 String suggest = map.get("suggest");//建议
		 String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		
		try {
			FeedBack fb = this.feedBackService.queryFeedBackByExpert(expertId, schoolId);
			if(fb == null){
				fb = new FeedBack();
			}
			fb.setExpertId(expertId);
			fb.setSchoolId(schoolId);
			fb.setImpression(impression);
			fb.setCharacteristic(characteristic);
			fb.setSuggest(suggest);
			fb.setSurvey(survey);
			fb.setState("1");
			fb.setCreateTime(nowTime);
			this.feedBackService.saveFeedBack(fb);
		
			return toResponse(null, ReturnCode.SUCCESS, "保存成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "保存失败");
		}
	}
	
	/**
	 * 查看某个评估反馈意见表
	 */
	@RequestMapping(value = "/queryOneFeedBack" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryOneFeedBack(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId= map.get("expertId");
		if(StringUtils.isEmpty(expertId)) {
			expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
		}
		String schoolId = map.get("schoolId");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "schoolId不能为空");
		}
		try {
			FeedBack feedback = this.feedBackService.queryFeedBackByExpert(expertId, schoolId);
			FeedBackBean bean=new FeedBackBean();
			BeanUtils.copyProperties(feedback,bean);
			Expert et=observationService.queryExpertInfo(expertId);
			bean.setExpertName(et.getS_name());
			return toResponse(bean, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}
	
	/**
	 * 查询学校名称
	 */
	@RequestMapping(value = "/querySchoolName" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> querySchoolName(HttpServletRequest request,@RequestBody Map<String,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);//专家id
	
		String schoolId = map.get("schoolId");
		if(null == expertId){
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		}
		if(schoolId == null || StringUtils.isBlank(schoolId)){
			return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校id不能为空");
		}
		try {
			School school = this.schoolService.queryNameById(schoolId);
		
			return toResponse(school, ReturnCode.SUCCESS, "查询成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "查询失败");
		}
	}
}
